package com.ghost.alibabaghost.web.controller.sys;

import com.ghost.alibabaghost.api.service.sys.SysRoleMenuService;
import com.ghost.alibabaghost.api.model.sys.SysRoleMenuModel;
import com.ghost.alibabaghost.result.SingleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/15 09:39
 */
@RestController
public class SysRoleMenuController {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleMenuController.class);
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * @param roleId
     * @return
     */
    @RequestMapping("getRoleMenuByRoleId")
    public SingleResult<String[]> getRoleMenuByRoleId(@RequestParam("roleId") Integer roleId) {
        logger.info("getRoleMenuByRoleId-->param:roleId=" + roleId);
        try {
            SysRoleMenuModel sysRoleMenuModel = sysRoleMenuService.queryRoleMenuByRoleId(roleId);
            String menuNodeId = sysRoleMenuModel.getMenuNodeId();
            if (sysRoleMenuModel != null) {
                return SingleResult.buildSuccess(menuNodeId.split(","));
            }
            return SingleResult.buildFailure();
        } catch (Exception e) {
            logger.error("getRoleMenuByRoleId-->param:roleId=" + roleId, e);
            return SingleResult.buildFailure();
        }
    }

    @RequestMapping("updateRoleMenu")
    public SingleResult<String> updateRoleMenu(@RequestBody SysRoleMenuModel sysRoleMenuModel) {
        logger.info("updateRoleMenu-->param:sysRoleMenuModel=" + sysRoleMenuModel);
        sysRoleMenuModel.setMenuNodeId(Arrays.toString(sysRoleMenuModel.getMenuRoleIds()));
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < sysRoleMenuModel.getMenuRoleIds().length; i++) {
            sb.append(sysRoleMenuModel.getMenuRoleIds()[i]);
            if (i != sysRoleMenuModel.getMenuRoleIds().length - 1) sb.append(",");
        }
        sysRoleMenuModel.setMenuNodeId(sb.toString());
        try {
            sysRoleMenuService.updateRoleMenu(sysRoleMenuModel);
            return SingleResult.buildSuccess("更新成功");
        } catch (Exception e) {
            logger.error("updateRoleMenu-->", e);
            return SingleResult.buildFailure();
        }
    }

    @RequestMapping("saveRoleMenu")
    public SingleResult<String> saveRoleMenu(@RequestBody SysRoleMenuModel sysRoleMenuModel) {
        logger.info("saveRoleMenu-->param:sysRoleMenuModel=" + sysRoleMenuModel);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < sysRoleMenuModel.getMenuRoleIds().length; i++) {
            sb.append(sysRoleMenuModel.getMenuRoleIds()[i]);
            if (i != sysRoleMenuModel.getMenuRoleIds().length - 1) sb.append(",");
        }
        sysRoleMenuModel.setMenuNodeId(sb.toString());
        try {
            sysRoleMenuService.insertRoleMenu(sysRoleMenuModel);
            return SingleResult.buildSuccess("新增成功");
        } catch (Exception e) {
            logger.error("updateRoleMenu-->", e);
            return SingleResult.buildFailure();
        }
    }
}
